package com.klun.likou;

import com.klun.likou.base.ListNode;

import java.util.ArrayList;
import java.util.List;

public class Solution {

	public static void main(String[] args) throws Exception {
//		ListNode head = genNode();
//		ListNode result = reverseKGroup(head, 3);
//		result.print();
		// int[][] matrix = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
		// int[][] matrix = {{1, 2}, {5, 6}, {9, 10}};
		int[][] matrix = {{1, 2},{ 3, 4}};
		List<Integer> integers = spiralOrder(matrix);
		for (Integer integer : integers) {
			System.out.print(integer + ",");
		}
		System.out.println();
	}


	/**
	 * 给定一个包含 m x n 个元素的矩阵（m 行, n 列），
	 * 请按照顺时针螺旋顺序，返回矩阵中的所有元素。
	 *
	 * @param matrix
	 * @return
	 */
	public static List<Integer> spiralOrder(int[][] matrix) {
		List<Integer> result = new ArrayList<>();
		int h = matrix.length;
		if (h <= 0) return result;
		int l = matrix[0].length;
		if (l <= 0) return result;
		int hang = h;
		int lie = l;
		int lz = 1; // 1:增 -1减 0:不变
		int hz = -1; // 1:增 -1减 0:不变
		int j = 0; // 列
		int i = 0; //行
		int count = h * l;
		// 分别是 列增-->行增-->列减-->行减-->列增-->行增-->列减
		for (; ; ) {
			if (count <= 0) break;
			// 列增
			if (lz == 1 && hz == -1) {
				for (; j < lie; j++) {
					result.add(matrix[i][j]);
					count--;
				}
				j--;
				i++;
				hz = 1;
				print(result);
			}
			// 行增
			else if (lz == 1 && hz == 1) {
				for (; i < hang; i++) {
					result.add(matrix[i][j]);
					count--;
				}
				i--;
				lz = -1;
				print(result);
			}
			// 列减
			else if (lz == -1 && hz == 1) {
				for (; j > l - lie; j--) {
					result.add(matrix[i][j - 1]);
					count--;
				}
				hz = -1;
				hang--;
				print(result);
			}
			// 行减
			else if (lz == -1 && hz == -1) {
				for (; i > h - hang; i--) {
					result.add(matrix[i - 1][j]);
					count--;
				}
				j++;
				lz = 1;
				lie--;
				print(result);
			}
		}
		return result;
	}

	private static void print(List<Integer> result) {
		for (Integer integer : result) {
			System.out.print(integer + ",");
		}
		System.out.println();
	}

	public static ListNode reverseKGroup(ListNode head, int k) {
		if (k <= 1) {
			return head;
		}
		if (!head.hasNext()) {
			return head;
		}
		ListNode prev = head;
		ListNode tail = null;
		ListNode a = head;
		ListNode b = head.next();
		int count = 0;
		do {
			if (null == b) {
				break;
			}
			count++;
			tail = b.next();
			if (count % k != 0) {
				b.next(a);
				a = b;
				b = tail;
			} else {
				if (count == k) {
					head = a;
				}
				prev.next(b);
				prev = a;
				ListNode c = b;
				for (int i = 0; i < k - 1; i++) {
					if (!c.hasNext()) {
						c = null;
						break;
					}
					c = c.next();
				}
				if (null == c) {
					break;
				}
				a = b;
				a.next(null);
				if (tail == null) {
					b = null;
				} else {
					b = tail;
				}

			}
		} while (true);
		return head;
	}

	public static ListNode genNode() {
		ListNode node = new ListNode(1);
		node.nextNode(2).nextNode(3).nextNode(4).nextNode(5).nextNode(6);
		return node;
	}
}
